﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using OGS.Contracts;

namespace OGS.Utility
{

    /// <summary>
    /// This is a helper class for various utility methods
    /// </summary>
    public static class Helper
    {

        /// <summary>
        /// Removes the HTML tags.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static string RemoveHTMLTags(string source)
        {
            return Regex.Replace(source, "<.*?>", string.Empty);
        }

        /// <summary>
        /// Get Query string value
        /// </summary>
        /// <param name="Key">The Key</param>
        /// <returns></returns>
        public static string GetQueryStringValue(string Key)
        {
            string strValue = string.Empty;
            if (!ReferenceEquals(HttpContext.Current.Request.QueryString[Key], null))
            {
                strValue = HttpContext.Current.Request.QueryString[Key].ToString();
            }
            return strValue;
        }

        /// <summary>
        /// Get Query string value in int
        /// </summary>
        /// <param name="Key">The Key</param>
        /// <returns></returns>
        public static int GetQueryStringValueInt(string Key)
        {
            int intValue = 0;
            if (!ReferenceEquals(HttpContext.Current.Request.QueryString[Key], null))
            {
                int.TryParse(HttpContext.Current.Request.QueryString[Key].ToString(), out intValue);
            }
            return intValue;
        }

        /// <summary>
        /// Check if the user agent is seacrh engine crawler
        /// </summary>
        public static bool IsSearchEngineCrawler
        {
            get
            {
                return Regex.IsMatch(HttpContext.Current.Request.UserAgent, @"bot|crawler|baiduspider|80legs|ia_archiver|voyager|curl|wget|yahoo! slurp|mediapartners-google", RegexOptions.IgnoreCase);
            }

        }

        /// <summary>
        /// Get escape fragment url
        /// </summary>
        /// <returns></returns>
        public static string GetEscapeFragmentFromURL()
        {
            string strEscapeFragment = string.Empty;

            if (!ReferenceEquals(HttpContext.Current.Request.QueryString["_escaped_fragment_"], null))
            {
                strEscapeFragment = HttpContext.Current.Request.QueryString["_escaped_fragment_"].ToString();
            }

            return strEscapeFragment;
        }

        /// <summary>
        /// Appends the parameter in URL.
        /// </summary>
        /// <param name="URL">The URL.</param>
        /// <param name="Key">The key.</param>
        /// <param name="Value">The value.</param>
        /// <returns></returns>
        public static string AppendParamInURL(string URL, string Key, string Value)
        {
            var uriBuilder = new UriBuilder(URL);
            var query = HttpUtility.ParseQueryString(uriBuilder.Query);
            query[Key] = Value;
            uriBuilder.Query = query.ToString();
            URL = uriBuilder.ToString();
            return URL;
        }
        
        /// <summary>
        /// Removes the parameter from URL.
        /// </summary>
        /// <param name="URL">The URL.</param>
        /// <param name="Key">The key.</param>
        /// <returns></returns>
        public static string RemoveParamFromURL(string URL, string Key)
        {
            string strURL = string.Empty;
            string[] separateURL = URL.Split('?');
            NameValueCollection queryString = new NameValueCollection();
            if (separateURL.Length > 0)
            {
                queryString = System.Web.HttpUtility.ParseQueryString(separateURL[1]);
            }

            if (queryString[Key] != null)
            {
                queryString.Remove(Key);
            }

            strURL = separateURL[0] + "?" + queryString.ToString();

            return strURL;
        }
        
        /// <summary>
        /// Renders the header.
        /// </summary>
        /// <param name="Text">The text.</param>
        /// <param name="EnableSorting">if set to <c>true</c> [enable sorting].</param>
        /// <param name="ColumnNo">The column no.</param>
        /// <param name="Rowspan">The rowspan.</param>
        /// <param name="Colspan">The colspan.</param>
        /// <returns></returns>
        public static string RenderHeader(string Text, bool EnableSorting, string Width, int ColumnNo, int Rowspan = 1, int Colspan = 1)
        {
            string strHTML = string.Empty;
            string strURL = HttpContext.Current.Request.Url.ToString();
            string strLink = string.Empty;
            int intSortColumnIndex = GetQueryStringValueInt("SortColumnIndex");
            int intSortDirection = GetQueryStringValueInt("SortDirection");
            string strClass = string.Empty;

            if (EnableSorting)
            {
                strLink = AppendParamInURL(strURL, "SortColumnIndex", ColumnNo.ToString());
                strLink = AppendParamInURL(strLink, "SortDirection", "0");
                strClass = "sorting";

                if (ColumnNo == intSortColumnIndex)
                {
                    if (intSortDirection == 0)
                    {
                        strLink = AppendParamInURL(strLink, "SortDirection", "1");
                        strClass = "sorting_asc";
                    }
                    else
                    {
                        strClass = "sorting_desc";
                    }

                }

                strHTML = "<th width=\"" + Width + "\" class=\"" + strClass + "\"" + "  colspan=\"" + Colspan.ToString() + 
                          "\" rowspan=\"" + Rowspan.ToString() + "\">" +
                          "<a style=\"display:block;\" href=\"" + strLink + "\">" + Text +  "</a></th>";

            }
            else
            {
                strHTML = "<th width=\"" + Width + "\" colspan=\"" + Colspan.ToString() + "\" rowspan=\"" + Rowspan.ToString() + "\">" + Text + "</th>";
            }

            return strHTML;
        }

        /// <summary>
        /// Get Current UserID
        /// </summary>
        /// <returns></returns>
        public static int GetCurrentUserID()
        {
            return 1;
        }

        /// <summary>
        /// Get Page Title
        /// </summary>
        /// <param name="Title"></param>
        /// <returns></returns>
        public static string GetPageTitle(string Title)
        {
            return Title + " - " + "mudikhana.com";
        }

	    /// <summary>
        /// Get Result count string
        /// </summary>
        /// <param name="SearchAttributes"></param>
        /// <returns></returns>
        public static string GetResultCount(SearchAttributes SearchAttributes)
        {
            string result = "Showing {0}-{1} of {2}";
            string startResult = (SearchAttributes.CurrentPageNumber * SearchAttributes.RecordsPerPage - 9).ToString();
            string endResult = string.Empty;
            if (SearchAttributes.TotalRecordCount > SearchAttributes.CurrentPageNumber * SearchAttributes.RecordsPerPage)
            {
                endResult = (SearchAttributes.CurrentPageNumber * SearchAttributes.RecordsPerPage).ToString();
            }
            else
            {
                endResult = SearchAttributes.TotalRecordCount.ToString();
            }

            if (SearchAttributes.TotalRecordCount == 0)
            {
                startResult = "0";
            }

            result = string.Format(result, startResult, endResult, SearchAttributes.TotalRecordCount.ToString());
            return result;
        }
    }
}
